package jlb.andriva;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

//import nati.droid.wrappers.ClosureExpiration;
import jlb.andriva.wrappers.HttpTask;
import jlb.andriva.wrappers.Limits;
import jlb.andriva.wrappers.Util;

public class LimitsActivity extends Fragment {

    private View rootView;
    private Element rootElem = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.activity_limits, container, false);

        if(rootElem == null){
        	if(LoginActivity.mTestMode == false){
	            LimitsTask mLimitsTask = new LimitsTask();
	            mLimitsTask.execute(getString(R.string.limits_url));
        	}
        	else{
        		Document doc = org.jsoup.Jsoup.parse(Util.getRawContent(rootView.getContext(), R.raw.limits));
                rootElem = doc.select("table[border=0]").first();
                if(rootElem != null){
                    paintResults();
                }
        	}
        }
        else{
            //Paint cache results
            paintResults();
        }

        return rootView;
    }

    private class LimitsTask extends HttpTask {
        @Override
        protected void onPostExecute(final String result) {
            if (result != null) {
                Document doc = org.jsoup.Jsoup.parse(result);
                rootElem = doc.select("table[border=0]").first();
                if(rootElem != null){
                    paintResults();
                }
            }
        }
    }

    private void paintResults(){
        ViewGenerator vg = new ViewGenerator();
        vg.execute(rootView.getContext());
    	
    	/*Limits lim = new Limits(rootElem);
        LinearLayout ll = (LinearLayout) rootView.findViewById(R.id.limits_list);
        ll.addView(lim.getView(rootView.getContext(), infoHandler));*/
    }

    private View.OnClickListener infoHandler = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Limits lim = new Limits(rootElem);
            View popupView = lim.getInfo(view);
            final PopupWindow popupWindow = new PopupWindow(popupView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);            
            
            popupWindow.setAnimationStyle(R.style.PopupWindowAnimation);
            ImageView btnDismiss = (ImageView)popupView.findViewById(R.id.popup_btnclose);
            btnDismiss.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    popupWindow.dismiss();
                }
            });
            popupWindow.showAtLocation(rootView, Gravity.CENTER, 0, 0);
        }
    };

    private class ViewGenerator extends AsyncTask<Context, Void, LinearLayout> {

        @Override
        protected LinearLayout doInBackground(Context... contexts) {
            Limits lim = new Limits(rootElem);
            return lim.getView(contexts[0], infoHandler);
        }

        @Override
        protected void onPostExecute(final LinearLayout result) {
            LinearLayout ll = (LinearLayout) rootView.findViewById(R.id.limits_list);
            ll.addView(result);
        }
    }
}